package com.itheima.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

public class BeansFactory {
    public static Map<String,Object> beansMap = new HashMap<>();

    private BeansFactory() {
    }

    public static Object getBean(String beanName) {
        Object obj = beansMap.get(beanName);
        if (obj == null) {
            ResourceBundle bundle = ResourceBundle.getBundle("beans");
            String classPath = bundle.getString(beanName);
            try {
                obj = Class.forName(classPath).newInstance();
                beansMap.put(beanName,obj);
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        return obj;
    }
}
